<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of File
 *
 * @author Sebastian
 */
class Common_System_File
{
    public function getFileType($filename) {
		$filename = strtolower($filename);// file name is changed to lowercase
		ereg( ".*\.([a-zA-z0-9]{0,5})$", $filename, $regs ); //gets extension of file
		$f_ext = $regs[1];

        $cont_type = null;

		switch ($f_ext) {//known file types

		   case "jpg": $cont_type = "image/jpeg"; break;
		   case "jpeg": $cont_type = "image/jpeg"; break;
		   case "gif": $cont_type = "image/gif"; break;
		   case "png": $cont_type = "image/png"; break;
		   case "txt": $cont_type = "text/plain"; break;
		   case "html": $cont_type = "text/html"; break;
		   case "htm": $cont_type = "text/html"; break;
		   case "doc": $cont_type = "application/msword";break;
		   case "exe": $cont_type = "application/octet-stream";break;
		   case "pdf": $cont_type = "application/pdf";break;
		   case "ai": $cont_type = "application/postscript";break;
		   case "eps": $cont_type = "application/postscript";break;
		   case "ps": $cont_type = "application/postscript";break;
		   case "xls": $cont_type = "application/vnd.ms-excel";break;
		   case "ppt": $cont_type = "application/vnd.ms-powerpoint";break;
		   case "zip": $cont_type = "application/zip";break;
		   case "mid": $cont_type = "audio/midi";break;
		   case "kar": $cont_type = "audio/midi";break;
		   case "mp3": $cont_type = "audio/mpeg";break;
		   case "wav": $cont_type = "audio/x-wav";break;
		   case "bmp": $cont_type = "image/bmp";break;
		   case "tiff": $cont_type = "image/tiff";break;
		   case "tif": $cont_type = "image/tiff";break;
		   case "asc": $cont_type = "text/plain";break;
		   case "rtf": $cont_type = "text/rtf";break;
		   case "mpeg": $cont_type = "video/mpeg";break;
		   case "mpg": $cont_type = "video/mpeg";break;
		   case "mpe": $cont_type = "video/mpeg";break;
		   case "avi": $cont_type = "video/x-msvideo";break;

		}


		return $cont_type; //returns the type
	}

	function upload( $file , $destination, $id, $name  )
	{
		$destino =  $destination.$id."-".$file[$name]['name'];
		if( move_uploaded_file($file[$name]['tmp_name'], $destino) )
		{
    		echo "The file ".  basename( $_FILES[$name]['name']). " has been uploaded";
		} 
		else
		{
    		echo "There was an error uploading the file, please try again!";
		}
		exit;
	}
	
	
	function download($filename,$path)
    {

    	$filename = html_entity_decode($filename);
    	$source = str_replace("á","?", $filename);
    	$source = str_replace("é","?", $source);
    	$source = str_replace("í","?", $source);
    	$source = str_replace("ó","?", $source);
    	$source = str_replace("ú","?", $source);
		
		
		
        $cont_type = $this->getFileType($filename);

        $fp = @fopen($path . $source, 'r');
        if($fp)
        {

            header("Content-Disposition: attachment; filename=".$filename);

            if ($cont_type == "")
                header("Content-type: application/force-download");
            else
                header("Content-type: $cont_type");

            fpassthru($fp);

            die();
        }
        else
          echo "FALLO";
	}
}
?>
